package org.dromara.business.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.business.domain.vo.OrphanDetailsVo;
import org.dromara.business.domain.bo.OrphanDetailsBo;
import org.dromara.business.service.OrphanDetailsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 孤儿详细信息
 *
 * @author zlj
 * @date 2025-01-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/orphan")
public class OrphanDetailsController extends BaseController {

    private final OrphanDetailsService orphanDetailsService;

    /**
     * 查询孤儿详细信息列表
     */
//    @SaCheckPermission("business:orphan:list")
    @GetMapping("/list")
    public TableDataInfo<OrphanDetailsVo> list(OrphanDetailsBo bo, PageQuery pageQuery) {
        return orphanDetailsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出孤儿详细信息列表
     */
//    @SaCheckPermission("business:orphan:export")
    @Log(title = "孤儿详细信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OrphanDetailsBo bo, HttpServletResponse response) {
        List<OrphanDetailsVo> list = orphanDetailsService.queryList(bo);
        ExcelUtil.exportExcel(list, "孤儿详细信息", OrphanDetailsVo.class, response);
    }

    /**
     * 获取孤儿详细信息详细信息
     *
     * @param userId 主键
     */
//    @SaCheckPermission("business:orphan:query")
    @GetMapping("/{userId}")
    public R<OrphanDetailsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long userId) {
        return R.ok(orphanDetailsService.queryById(userId));
    }

    /**
     * 新增孤儿详细信息
     */
//    @SaCheckPermission("business:orphan:add")
    @Log(title = "孤儿详细信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OrphanDetailsBo bo) {
        return toAjax(orphanDetailsService.insertByBo(bo));
    }

    @Log(title = "孤儿详细信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/sum")
    public R<Void> sum(@Validated(AddGroup.class) @RequestBody OrphanDetailsBo bo) {
        return toAjax(orphanDetailsService.add(bo));
    }
    /**
     * 修改孤儿详细信息
     */
//    @SaCheckPermission("business:orphan:edit")
    @Log(title = "孤儿详细信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OrphanDetailsBo bo) {
        return toAjax(orphanDetailsService.updateByBo(bo));
    }

    /**
     * 删除孤儿详细信息
     *
     * @param userIds 主键串
     */
//    @SaCheckPermission("business:orphan:remove")
    @Log(title = "孤儿详细信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] userIds) {
        return toAjax(orphanDetailsService.deleteWithValidByIds(List.of(userIds), true));
    }
}
